unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, rtcInfo, rtcConn, rtcTcpSrv, StdCtrls;

type
  TForm1 = class(TForm)
    ChkBox_1: TCheckBox;
    TcpServer_1: TRtcTcpServer;
    procedure ChkBox_1Click(Sender: TObject);
    procedure TcpServer_1DataReceived(Sender: TRtcConnection);
    procedure FormShow(Sender: TObject);
  private
    FReadedTxt:string;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzDebug,Class_KzUtils;

{$R *.dfm}

procedure TForm1.ChkBox_1Click(Sender: TObject);
begin
  if TCheckBox(Sender).Checked then
  begin
    TcpServer_1.ServerAddr:='192.168.0.51';
    TcpServer_1.ServerPort:='1861';
    TcpServer_1.Listen();
  end else
  begin
    if TcpServer_1.isListening then
    begin
      TcpServer_1.StopListen;
    end;
  end;
end;

procedure TForm1.TcpServer_1DataReceived(Sender: TRtcConnection);
var
  TextA:string;
  TempA:string;
  NumbA:Integer;
begin
  if Sender.inMainThread then
  begin
    TextA:=Sender.Read;
    FReadedTxt:=FReadedTxt+TextA;
    TempA:=Copy(FReadedTxt,1,6);
    NumbA:=StrToIntDef(TempA,0);
    KzDebug.FileFmt('readcount:%D',[Sender.ReadCount]);
    if Length(FReadedTxt)=NumbA then
    begin
      KzDebug.FileLog(Length(FReadedTxt));
      FReadedTxt:='';
    end;
  end else
  begin
    Sender.Sync(TcpServer_1DataReceived);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  TcpServer_1.MultiThreaded:=True;
end;

end.
